/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Notes (4/24/2024)
	We obtained 1-year age-specific arrest data from the Supreme Prosecutor's Office of Korea through a request made to the Open Data Mediation Committee for research purposes. 
	(For more details on the request process, please visit https://www.data.go.kr/en/index.do)
	However, we do not have permission to redistribute this data. 
	Consequently, the data file does not contain the age-specific arrest data used in our analysis. 
	Nonetheless, it will show how we structured our age-period-cohort data, along with the population data used for analysis. 
	The Stata do-file will show how we conduct our analysis. 
	Please do not hesitate to contact the corresponding author with any questions or if you find any errors in our data and code.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Data
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
use APC_Criminology, clear
	sort year age cohort
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Table 1 Age-period-cohort characteristic model of logged age-specific arrest rates by types of crime on relative cohort size (RCS), age group, and period 1979–2019
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
reg ln_total_rates ageeffect1-ageeffect11 pereffect1-pereffect9 ln_rcc
reg ln_homicide_rates ageeffect1-ageeffect11 pereffect1-pereffect9 ln_rcc
reg ln_proindex_rates ageeffect1-ageeffect11 pereffect1-pereffect9 ln_rcc
	
* ln_total_rates = logged age-specific arrest rates (total crime)
* ln_homicide_rates = logged age-specific arrest rates (homicide)
* ln_proindex_rates = logged age-specific arrest rates (property crime index)
* ln_rcc = logged relative cohort size

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Table 2 The linear trend of residuals within cohorts
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
reg ln_total_rates  ageeffect1_2 ageeffect3-ageeffect11 pereffect1-pereffect9 coheffect5-coheffect19
	predict r1_total, residual
	forval i= 9/12{
	reg	r1_total period if cohort==`i'
	}	

reg ln_homicide_rates  ageeffect1_2 ageeffect3-ageeffect11 pereffect1-pereffect9 coheffect5-coheffect19
	predict r1_homicide, residual
	forval i= 9/12{
	reg	r1_homicide period if cohort==`i'
	}
		
reg ln_proindex_rates  ageeffect1_2 ageeffect3-ageeffect11 pereffect1-pereffect9 coheffect5-coheffect19
	predict r1_proindex, residual		
	forval i= 9/12{
	reg	r1_proindex period if cohort==`i'
	}

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Table 3 The studentized residuals for cohort-period residuals from the APCMC model of logged arrest rates
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
forval i= 9/12{
reg r1_total period if cohort==`i'
	predict stur_total_c`i', rstudent
	list year stur_total_c`i' if cohort==`i'	
	}	
		
forval i= 9/12{
reg r1_homicide period if cohort==`i'
	predict stur_homicide_c`i', rstudent
	list year stur_homicide_c`i' if cohort==`i'	
	}		
		
forval i= 9/12{
reg r1_proindex period if cohort==`i'
	predict stur_proindex_c`i', rstudent
	list year stur_proindex_c`i' if cohort==`i'	
	}			
		
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Figure 7 The stability of cohort effects across periods
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
* Step 1: Run a regression using a constraint (e.g., age1=age2)
reg ln_total_rates  ageeffect1_2 ageeffect3-ageeffect11 pereffect1-pereffect9 coheffect5-coheffect19
reg ln_homicide_rates  ageeffect1_2 ageeffect3-ageeffect11 pereffect1-pereffect9 coheffect5-coheffect19
reg ln_proindex_rates  ageeffect1_2 ageeffect3-ageeffect11 pereffect1-pereffect9 coheffect5-coheffect19

* Step 2: Regress the cohort coefficients from the younggest to the older age on the numbers 1 to I
** Step2-1 Input cohort coefficients
input linear	total_coh	homicide_coh	proindex_coh
1	2.711433	4.172194	-2.149984
2	2.49873	3.975479	-1.702938
3	2.227754	3.49322	-1.321122
4	1.851548	2.885084	-1.093398
5	1.563856	2.440958	-0.6982193
6	1.141072	1.856588	-0.4153669
7	0.6331806	1.193897	-0.199155
8	0.0890103	0.4269593	-0.0033619
9	-0.5351675	-0.4662049	0.1222279
10	-0.9776513	-1.394412	0.3731934
11	-1.453003	-2.25189	0.6567972
12	-2.054332	-3.103097	0.9048222
13	-2.291869	-3.95123	1.510462
14	-2.592718	-4.277618	1.935737
15	-2.8118431	-4.9999274	2.0803054
end
** Step 2-2: Run regression
reg total_coh linear
		predict r_total_coh, residual
		list r_total_coh if linear >=5 & linear <=8
		/*1951-1955.	.2578957	
		  1956-1960.	.2704318	
		  1961-1965.	.1978605	
		  1966-1970.	.0890103  */
reg homicide_coh linear
		predict r_homicide_coh, residual
		list r_homicide_coh if linear >=5 & linear <=8
		/*1951-1955.	.3287539	
		  1956-1960.	.4484519	
		  1961-1965.	.489829	
		  1966-1970.	.4269593	*/
reg proindex_coh linear
		predict r_proindex_coh, residual
		list r_proindex_coh if linear >=5 & linear <=8		  
		/*1951-1955.	.1541924	
		  1956-1960.	.1529076	
		  1961-1965.	.0849822	
		  1966-1970.	-.0033619 */	

* Step 3: Add the deviation of the cohort effects from the linear trend to each residuals
gen r1_devc_total=.
	replace r1_devc_total= r1_total+.2578957 if cohort==9
	replace r1_devc_total= r1_total+.2704318 if cohort==10
	replace r1_devc_total= r1_total+.1978605 if cohort==11
	replace r1_devc_total= r1_total+.0890103 if cohort==12
	
gen r1_devc_homicide=.
	replace r1_devc_homicide= r1_homicide+.3287539 if cohort==9
	replace r1_devc_homicide= r1_homicide+.4484519 if cohort==10
	replace r1_devc_homicide= r1_homicide+.48982897 if cohort==11
	replace r1_devc_homicide= r1_homicide+.4269593 if cohort==12

gen r1_devc_proindex=.
	replace r1_devc_proindex= r1_proindex+.1541924 if cohort==9
	replace r1_devc_proindex= r1_proindex+.1529076 if cohort==10
	replace r1_devc_proindex= r1_proindex+.0849822 if cohort==11
	replace r1_devc_proindex= r1_proindex-.0033619 if cohort==12
	
* Step 4: Extract the values
forval i= 9/12{
list year r1_total r1_devc_total if cohort==`i'
	}	
	
forval i= 9/12{
list year r1_homicide r1_devc_homicide if cohort==`i'
	}	
		
forval i= 9/12{
list year r1_proindex r1_devc_proindex if cohort==`i'
	}	
		
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Supplementary Material 1  
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
* Table S1
reg ln_total_rates  ageeffect1-ageeffect11 pereffect1_2 pereffect3-pereffect9 coheffect5-coheffect19
	predict r2_total, residual
reg ln_total_rates  ageeffect1-ageeffect11 pereffect1-pereffect9 coheffect5_6 coheffect7-coheffect19
	predict r3_total, residual

* Table S2
forval i= 9/12{
list r1_total r2_total r3_total if cohort==`i'
	}	
	*Values are identical
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Supplementary Material 2  
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
* Table S3
reg ln_total_rates ageeffect1-ageeffect11 pereffect1-pereffect9 ln_rcc ln_sexratio
reg ln_homicide_rates ageeffect1-ageeffect11 pereffect1-pereffect9 ln_rcc ln_sexratio
reg ln_proindex_rates ageeffect1-ageeffect11 pereffect1-pereffect9 ln_rcc ln_sexratio
	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Supplementary Material 4  
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
* Table S6
reg ln_larceny_rates ageeffect1-ageeffect11 pereffect1-pereffect9 ln_rcc
reg ln_fraud_rates ageeffect1-ageeffect11 pereffect1-pereffect9 ln_rcc

* Table S7
reg ln_larceny_rates  ageeffect1_2 ageeffect3-ageeffect11 pereffect1-pereffect9 coheffect5-coheffect19
	predict r1_larceny, residual
	forval i= 9/12{
	reg	r1_larceny period if cohort==`i'
	}	

reg ln_fraud_rates  ageeffect1_2 ageeffect3-ageeffect11 pereffect1-pereffect9 coheffect5-coheffect19
	predict r1_fraud, residual
	forval i= 9/12{
	reg	r1_fraud period if cohort==`i'
	}	

	
		
